package com.beijing.demo.binary;

/**
 * 曹旭
 * 2025/4/21
 * 二进制 :
 * 输出int类型的32位
 */
public class PrintBinaryTest {
    /**
     * 自己写的方法
     * @param num
     * @return
     */
    public static String printBinary(int num){
        StringBuffer buffer = new StringBuffer();
        for (int i = 31; i >= 0; i--) {
            buffer.append((num & (1 << i)) == 0 ? "0" : "1");
        }
        return buffer.toString();
    }

    /**
     * 网上找的方法
     * @param num
     * @return
     */
    public static String to32BitBinary(int num) {
        StringBuilder binary = new StringBuilder(Integer.toBinaryString(num));
        while (binary.length() < 32) {
            binary.insert(0, '0');
        }
        return binary.toString();
    }

    public static void main(String[] args) {
        System.out.println("test start ~~~");
        int countNum = 10000000;
        for (int i = 0; i < countNum; i++) {
            String printBinary = printBinary(countNum);
            String to32BitBinary = to32BitBinary(countNum);
            if(!printBinary.equals(to32BitBinary)){
                System.out.println("fucking fuck~~~");
                break;
            }
        }
        System.out.println("test end ~~~");
    }
}
